package com.itcast.demo_one

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class ButtonActivity : AppCompatActivity(), View.OnClickListener {
    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_button)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        /**
         * 方式二: 获取到按钮, 绑定点击事件
         */
        /*val btn2 = findViewById<Button>(R.id.btn2)
        btn2.setOnClickListener {
            Toast.makeText(this, "点击了按钮2222", Toast.LENGTH_SHORT).show()
        }*/

        /**
         * 方式三: 通过实现 View.OnClickListener 接口, 绑定点击事件
         */
        findViewById<Button>(R.id.btn3).setOnClickListener(this)
        findViewById<Button>(R.id.btn2).setOnClickListener(this)
    }

    /**
     * 方式一
     */
    fun onBtnClick(view: View){
        Toast.makeText(this, "点击了按钮1111", Toast.LENGTH_SHORT).show()
    }

    override fun onClick(v: View?) {
        when(v?.id){
            R.id.btn3 -> Toast.makeText(this, "点击了按钮3333", Toast.LENGTH_SHORT).show()
            R.id.btn2 -> Toast.makeText(this, "点击了按钮2222", Toast.LENGTH_SHORT).show()
        }
    }
}